import 'package:meta/meta.dart';

class Deserialized<T> {
  final T value;
  final int readBytes;
  Deserialized(this.value, this.readBytes);
}

abstract class CacheSerializer<T> {
  List<int> serialize(T value);
  Deserialized<T> deserialize(List<int> data);

  CacheSerializer();

  factory CacheSerializer.builder({
     @required ByteDeserializer<T> deserializer,
     @required ByteSerializer<T> serializer,
  }) {
    return _LambdaCacheSerializer(deserializer: deserializer, serializer: serializer);
  }
}

typedef Deserialized<T> ByteDeserializer<T>(List<int> data);

typedef List<int> ByteSerializer<T>(T value);

class _LambdaCacheSerializer<T> extends CacheSerializer<T> {
  
  final ByteDeserializer<T> deserializer;

  final ByteSerializer<T> serializer;

  _LambdaCacheSerializer({@required this.deserializer, @required this.serializer});

  @override
  Deserialized<T> deserialize(List<int> data) {
    return deserializer(data);
  }

  @override
  List<int> serialize(T value) {
    return serializer(value);
  }
  

}